'use strict';

const db = uniCloud.database()
exports.main = async (event, context) => {
	let { relateUuid, wordBankId } = event;
	try {
		// 查询用户某个词库的背单词状态
		const res = await db.collection('memorization').where({
			relateUuid,
			wordBankId,
		}).get();
		
		const forget = res.data[0].studyedWords.filter(item  => item.forget === true).length; // 获取数组长度
		const num1 = res.data[0].studyedWords.filter(item  => item.studyedNum === 1).length; // 获取数组长度
		const num2 = res.data[0].studyedWords.filter(item  => item.studyedNum === 2).length; // 获取数组长度
		const num3 = res.data[0].studyedWords.filter(item  => item.studyedNum === 3).length; // 获取数组长度
		const num4 = res.data[0].studyedWords.filter(item  => item.studyedNum === 4).length; // 获取数组长度
		const num5 = res.data[0].studyedWords.filter(item  => item.studyedNum === 5).length; // 获取数组长度
		const num6 = res.data[0].studyedWords.filter(item  => item.studyedNum === 6).length; // 获取数组长度
		const known = res.data[0].studyedWords.filter(item  => item.known === true || item.studyedNum === 7).length; // 获取数组长度
		
		const todayStudyNum = res.data[0].todayStudyNum;
		const todayNeedReviewNum = res.data[0].todayNeedReviewNum;
		
		return {
		  code: 200,
		  message: '统计成功',
		  data: {
			  forget,
			  num1,
			  num2,
			  num3,
			  num4,
			  num5,
			  num6,
			  known,
			  todayStudyNum,
			  todayNeedReviewNum
		  }
		};
	} catch(err) {
		return {
		  code: 500,
		  message: '统计失败',
		  error: err
		};
	}
};
